package www.shuang.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

import www.shuang.common.controller.BaseController;
import www.shuang.common.util.ConstatFinalUtil;
import www.shuang.common.util.PageInfoUtil;
import www.shuang.investor.pojo.AInvestor;
import www.shuang.investor.service.IAInvestorService;
import www.shuang.system.pojo.ARegion;

@Controller
@RequestMapping("/back/investor/")
public class InvestorController extends BaseController
{		@Resource
		protected IAInvestorService investorService;
		@RequestMapping("/investorList.htm")
		public String investorList(HttpServletRequest request){
			//接收参数
			String keyword=request.getParameter("keyword");
			String status=request.getParameter("status");
			String st=request.getParameter("st");
			String ed=request.getParameter("ed");
			String coopType=request.getParameter("coopType");
			/*if(status == null){
				status="";
			}*/
			if(st == null){
				st="";
			}
			if(ed == null){
				ed="";
			}
			if(coopType == null){
				coopType="";
			}
			Date stDate = null;
			Date edDate= null;
			if(!"".equalsIgnoreCase(st)&&!"".equalsIgnoreCase(ed)){
				stDate=this.dateFormatUtil.parseDate(st);
				edDate=this.dateFormatUtil.parseDate(ed);
			}
			PageInfoUtil pageInfoUtil =this.proccedPage(request);
			Map<String, Object> condMap=new HashMap<String,Object>();
			condMap.put("keyword", keyword);
			condMap.put("status", status);
			condMap.put("stDate", stDate);
			condMap.put("edDate", edDate);
			condMap.put("coopType", coopType);
			List<AInvestor> investorList = this.investorService.findCondListAInvestorService(pageInfoUtil, condMap);
			//放入作用域中
			request.setAttribute("investorList", investorList);
			request.setAttribute("pageInfoUtil", pageInfoUtil);
			//
			request.setAttribute("keyword", keyword);
			request.setAttribute("status", status);
			request.setAttribute("st", st);
			request.setAttribute("ed", ed);
			request.setAttribute("coopType", coopType);
			
			return "/back/investor/investorList";
		}
		
		
		//更新内容
		@RequestMapping(value="/investorUpdate.htm",produces="text/html;charset=utf-8")
		public @ResponseBody String investorUpdate(String iid,HttpServletRequest request){
			ConstatFinalUtil.LOGGER_SYSTEM.info("---investorUpdate----");
			Map<String,Object> condMap=new HashMap<String,Object>();
			condMap.put("id", iid);
			AInvestor investor = this.investorService.findOneAInvestorService(condMap);
			request.setAttribute("investor", investor);
			return "/back/investor/investorUpdate";
		}
		
		//更新提交
		
		@RequestMapping("/investorUpdateSubmit.htm")
		public String investorUpdateSubmit(String id,HttpServletRequest request,MultipartFile file){
			
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("id", id);
		AInvestor investor = this.investorService.findOneAInvestorService(condMap);
			if(investor != null){
				//接收参数
				String code=request.getParameter("code");
				String name=request.getParameter("name");
				String url=request.getParameter("url");
				String contactName=request.getParameter("contactName");
				String contactPhone=request.getParameter("contactPhone");
				String logo=request.getParameter("logo");
				String content=request.getParameter("content");
				String totalNum=request.getParameter("totalNum");
				String succedNum=request.getParameter("succedNum");
				String coopType=request.getParameter("coopType");
				String status=request.getParameter("status");
				String pubTime=request.getParameter("pubTime");
				
				
				investor.setPubTime(this.dateFormatUtil.parseDate(pubTime));
				investor.setCode(code);
				investor.setName(name);
				investor.setUrl(url);
				investor.setContactName(contactName);
				investor.setContactPhone(contactPhone);
				investor.setLogo(logo);
				investor.setContent(content);
				investor.setTotalNum(Integer.valueOf(totalNum));
				investor.setSuccedNum(Integer.valueOf(succedNum));
				investor.setCoopType(Byte.valueOf(coopType));
				investor.setStatus(Byte.valueOf(status));
				investor.setUpdateTime(new Date());
				//处理文件上传需要的信息
				Map<String,Object> paramsMap=new HashMap<String,Object>();
				//上传文件
				paramsMap.put("file", file);
				//获取文件的真实路径
				paramsMap.put("truePath", request.getRealPath("/"));
				try
				{
					JSONObject resultJSON = this.investorService.updateOneAInvestorService(investor);
					this.info=resultJSON.getString("info");
				} catch (Exception e)
				{
					this.info="更新出资方失败了";
				}
			}else{
				this.info="当前出资方不存在";
			}
			JSONObject returnJuiJSON = this.returnJuiJSON(request, info);
		return returnJuiJSON.toJSONString();
		}
}
